package exam.yingjiao.S2;

import util.GraphUtil;

public class Main {

    public static void main(String[] args) {
        int[][] a = GraphUtil.parseGraph("[[2,1,3],[2,2,2],[2,1,4]]");
        int[][] attacks = GraphUtil.parseGraph("[[1,2],[2,2],[1,2],[3,3]]");
        System.out.println(new Main().numsOfMonsters(1, a, attacks));
    }

    public int numsOfMonsters(int k, int[][] a, int[][] attacks) {
        int[] dx = new int[] {-1, -1, -1, 0, 1, 1, 1, 0};
        int[] dy = new int[] {-1, 0, 1, 1, 1, 0, -1, -1};
        int m = a.length, n = a[0].length;
        int burn = 0;
        for (int i=0; i<attacks.length; ++i) {
            int x = attacks[i][0] - 1, y = attacks[i][1] - 1;
            if (a[x][y] <= 0)
                continue;
            if (burn < 2) {
                burn++;
                a[x][y] = Math.max(0, a[x][y] - k);
            } else {
                burn = 0;
                a[x][y] = Math.max(0, a[x][y] - (3*k));
                int range = (3*k) / 2;
                for (int j = 0; j < 8; j++) {
                    int nx = x + dx[j], ny = y + dy[j];
                    if (valid(m, n, nx, ny))
                        a[nx][ny] = Math.max(0, a[nx][ny] - range);
                }
            }
        }
        int num = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (a[i][j] > 0)
                    num++;
            }
        }
        return num;
    }

    private boolean valid(int m, int n, int x, int y) {
        if (x < 0 || x >= m || y < 0 || y >= n)
            return false;
        return true;
    }
}